package com.zhuizikejithreeig.cn.threeigkeji.loginfo;

import android.Manifest;
import android.os.Bundle;
import android.os.Vibrator;
import android.text.TextUtils;
import android.util.Log;
import android.widget.Toast;


import com.zhuizikejithreeig.cn.been.MacBeen;
import com.zhuizikejithreeig.cn.been.RegisterBeen;
import com.zhuizikejithreeig.cn.contect.ContectUtils;
import com.zhuizikejithreeig.cn.contect.UriContect;
import com.zhuizikejithreeig.cn.threeigkeji.BasicActivity;
import com.zhuizikejithreeig.cn.threeigkeji.CompontApplayCation;
import com.zhuizikejithreeig.cn.threeigkeji.MainActivity;
import com.zhuizikejithreeig.cn.threeigkeji.R;
import com.zhuizikejithreeig.cn.utils.GsonUtil;
import com.zhuizikejithreeig.cn.utils.ToastMake;

import com.zhuizikejithreeig.cn.utils.Utils;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.util.HashMap;
import java.util.Map;


import cn.bingoogolapple.qrcode.core.QRCodeView;
import cn.bingoogolapple.qrcode.zxing.ZXingView;
import kr.co.namee.permissiongen.PermissionGen;
import okhttp3.Call;
import okhttp3.MediaType;

public class SaoMaActivity extends BasicActivity implements QRCodeView.Delegate {

    private static final int REQUEST_CODE_CHOOSE_QRCODE_FROM_GALLERY = 666;

    private QRCodeView mQRCodeView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        showBody();
        setContentView(R.layout.activity_sao_ma);
        PermissionGen.with(SaoMaActivity.this)
                .addRequestCode(REQUEST_CODE_CHOOSE_QRCODE_FROM_GALLERY)
                .permissions(
                        Manifest.permission.CAMERA
                )
                .request();

        mQRCodeView = (ZXingView) findViewById(R.id.zxingview);
        mQRCodeView.setDelegate(this);

    }

    @Override
    protected void onStart() {
        super.onStart();
        mQRCodeView.startCamera();
        //        mQRCodeView.startCamera(Camera.CameraInfo.CAMERA_FACING_FRONT);

        mQRCodeView.showScanRect();
        //开始扫描二维码
        mQRCodeView.startSpot();
    }

    @Override
    protected void onStop() {
        mQRCodeView.stopCamera();
        super.onStop();
    }

    @Override
    protected void onDestroy() {
        mQRCodeView.onDestroy();
        super.onDestroy();
    }

    private void vibrate() {
        Vibrator vibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE);
        vibrator.vibrate(200);
    }

    @Override
    public void onScanQRCodeSuccess(String result) {
        //        Toast.makeText(this, result, Toast.LENGTH_SHORT).show();
        vibrate();
        mQRCodeView.startSpot();
        if (result != null || !TextUtils.isEmpty(result)) {
            mQRCodeView.stopSpot();

            sendMac(result);

        }
    }

    private void sendMac(String mac) {
        OkHttpUtils
                .get()
                .url(mac)
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        ToastMake.showToast(getResources().getString(R.string.string58));
                        //开始扫描二维码
                        mQRCodeView.startSpot();
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        MacBeen been = GsonUtil.parseJsonToBean(response, MacBeen.class);
                        if (been == null) {
                            ToastMake.showToast(getResources().getString(R.string.string58));
                            //开始扫描二维码
                            mQRCodeView.startSpot();
                            return;
                        }
                        if (!TextUtils.equals(been.getStatus(), "200")) {
                            ToastMake.showToast(been.getMsg());
                            //开始扫描二维码
                            mQRCodeView.startSpot();
                            return;
                        } else {
                            //扫描成功后停止扫描
                            mQRCodeView.stopSpot();
                            //扫描出来二维码,向后台传递二维码的信息
                            initData(been.getData().getMac());

                        }

                    }
                });

    }

    private void initData(String mac) {
        showProgrees("绑定座椅中.....");
        OkHttpUtils
                .post()
                .url(UriContect.INDEX)
                .addParams("phone", Utils.getString(ContectUtils.PHONESAVE))
                .addParams("mac", mac)
                .addParams("position", CompontApplayCation.postion)
                .addParams("id", Utils.getString(ContectUtils.IDSAVE))
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        hideProgree();
                        ToastMake.showToast(getResources().getString(R.string.string58));
                        //开始扫描二维码
                        mQRCodeView.startSpot();
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        hideProgree();
                        RegisterBeen been = GsonUtil.parseJsonToBean(response, RegisterBeen.class);

                        if (been == null) {
                            ToastMake.showToast(getResources().getString(R.string.string58));
                            //开始扫描二维码
                            mQRCodeView.startSpot();
                            return;
                        }
                        if (!TextUtils.equals(been.getStatus(), "200")) {
                            ToastMake.showToast(been.getMsg());
                            //开始扫描二维码
                            mQRCodeView.startSpot();
                            return;
                        } else {
                            mQRCodeView.stopSpot();
                            skipOtherActivity(MainActivity.class,true);
                            finish();
                        }
                    }
                });
    }

    @Override
    public void onScanQRCodeOpenCameraError() {
        //        Log.e(TAG, "打开相机出错");
    }

    //    private void getData(String mac) {
    //
    //
    //        Map<String, String> map = new HashMap<>();
    //        map.put("phone", CompontApplayCation.phone);
    //        map.put("mac", myMac);
    //        map.put("position", CompontApplayCation.postion);
    //
    //                String json = GsonUtil.parseMapToJson(map);
    //                OkHttpUtils
    //                        .postString()
    //                        .url(UriContect.INDEX)
    //                        .content(json)
    //                        .mediaType(MediaType.parse("application/json; charset=utf-8"))
    //                        .build()
    //                        .execute(new StringCallback() {
    //                            @Override
    //                            public void onError(Call call, Exception e, int id) {
    //                                ToastMake.showToast(getResources().getString(R.string.string58));
    //                                //开始扫描二维码
    //                                mQRCodeView.startSpot();
    //                            }
    //
    //                            @Override
    //                            public void onResponse(String response, int id) {
    //
    //                                RegisterBeen been = GsonUtil.parseJsonToBean(response, RegisterBeen.class);
    //
    //                                if (been==null){
    //                                    ToastMake.showToast(getResources().getString(R.string.string58));
    //                                    //开始扫描二维码
    //                                    mQRCodeView.startSpot();
    //                                    return;
    //                                }
    //                                if (!TextUtils.equals(been.getStatus(),"1")){
    //                                    ToastMake.showToast(been.getMsg());
    //                                    //开始扫描二维码
    //                                    mQRCodeView.startSpot();
    //                                    return;
    //                                }else {
    //                                    finish();
    //                                }
    //
    //
    //                            }
    //                        });
    //
    //    }


}
